import request from '@/router/axios';
import Mock from 'mockjs';

let _list = [
    {
        name:'早上秒杀场',
        startTime:'10:00:00',
        endTime:'10:30:00',
        id:1,
        status:"0",//0启用  1不启用
        num:'',
        children:[]
    },
    {
        name:'中午秒杀场',
        startTime:'12:00:00',
        endTime:'12:30:00',
        id:2,
        status:"0",
        num:'',
        children:[]
    },
    {
        name:'晚上秒杀场',
        startTime:'20:00:00',
        endTime:'20:30:00',
        id:3,
        status:"1",
        num:'',
        children:[]
    },
]

// 获取秒杀场次列表
export const sesList = (data) =>request({
    url:'/sesList',
    method:'get',
    data
})
Mock.mock(/\/sesList/,config=>{
    //总条数
    let total = _list.length;
    return {
        data:{
            _list,total
        },
        meta:{
            msg:'请求成功',
            status:200
        }
    }   
})
//添加秒杀场次
export const addSes = (data) =>request({
    url:'/addSes',
    mothed:'post',
    data
});
Mock.mock(/\/addSes/,config=>{
    var ids = Mock.mock({"id|4-20":1000})
    let datas = JSON.parse(config.body)
    let {name,startTime,endTime,status} = datas;
    if(name && startTime && endTime,status){
        _list.unshift({
            id:ids.id,
            name,
            startTime,
            endTime,
            status,
            children:[]
        })
        return {
            meta:{
                msg:'添加成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'添加失败',
                status:400
            }
        }
    }
    
})

//删除数据
export const detSes = (data) =>request({
    url:'/detSes',
    mothed:'delete',
    data
})
Mock.mock(/\/detSes/,config=>{
    let {id} = JSON.parse(config.body);
    if(id){
        for(let index in _list){
            if(_list[index].id==id){
                _list.splice(index,1)
            }
        }
        return {
            meta:{
                msg:'删除成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'删除失败',
                status:400
            }
        }
    }
    
})

// 修改数据
export const editSes = (data) =>request({
    url:'/editSes',
    mothed:'put',
    data
})
Mock.mock(/\/editSes/,config=>{ 
    let datas = JSON.parse(config.body);
    let {id} = datas;
    if(id){
        if(datas.name || datas.startTime || datas.endTime ){
            for(let item of _list){
                if(item.id==id){
                    item.name = datas.name?datas.name:item.name;
                    item.startTime = datas.startTime?datas.startTime:item.startTime
                    item.endTime = datas.endTime?datas.endTime:item.endTime;
                }
            }
        }
        return {
            meta:{
                msg:'修改成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
   
})
// 修改是否上线
export const editlines = (data) =>request({
    url:'/editline',
    mothed:'put',
    data
})
Mock.mock(/\/editline/,config=>{ 
    let datas = JSON.parse(config.body);
    let {id,status} = datas;
    if(id && status){
        _list[_list.findIndex(item=>item.id==id)].status = datas.status;
        return {
            meta:{
                msg:'修改成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
   
})